<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>贪吃蛇 | Hugo Theme Dream</title>

    



<meta name="author" content="Lindixuan" />
<meta name="description" content="" />



<meta name="generator" content="Hugo 0.63.2" />

<link rel="canonical" href="https://lindixuan.gitee.io/%E8%B4%AA%E5%90%83%E8%9B%87/" />


<meta property="og:title" content="贪吃蛇" />
<meta property="og:description" content="" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://lindixuan.gitee.io/%E8%B4%AA%E5%90%83%E8%9B%87/" />
<meta property="article:published_time" content="2020-09-20T12:48:58+08:00" />
<meta property="article:modified_time" content="2020-09-20T12:48:58+08:00" />



<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="贪吃蛇"/>
<meta name="twitter:description" content=""/>


<link rel="stylesheet" href="/css/semantic.min.css" />
<link rel="stylesheet" href="/css/OverlayScrollbars.min.css" />
<link rel="stylesheet" href="/css/github-markdown.css" />
<link rel="stylesheet" href="/css/site.css" />


<style>
  a {
    color: seagreen !important;
  }
</style>



<style>
  .inverted a {
     color: darkseagreen !important;
  }
</style>


    


  
    <link rel="stylesheet" data-highlight href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/styles/gruvbox-light.min.css" />
  



  </head>

  
  <body style="background: white;">
  
    
<nav class="ui secondary inverted menu dream-menu">

  <div class="item">
    <i class="large link bullseye icon dream-flip-toggle" title="Flip it!"></i>
  </div>
  <div class="item">
    <i class="large link home icon" title="Home" onclick="window.location.href = 'https:\/\/lindixuan.gitee.io'"></i>
  </div>
  <div class="item">
    <i class="large link icon theme-switch" onclick="themeSwitch()"></i>
  </div>
  
  <div class="item">
    <i class="large link search icon" onclick="toggleSearch()"></i>
  </div>
  
</nav>

    <div class="flip-container">
      <div class="flipper">
        <section class="front">
          <div class="dream-max-width">
            
<div class="ui centered relaxed grid dream-grid">
  <div class="sixteen wide mobile sixteen wide tablet twelve wide computer column markdown-body dream-single">

    <section class="ui top attached segment" id="dream-save-post-as-img">
      <header style="margin-top: 0 !important;">
        <h1 class="ui large header">
          贪吃蛇
          <div class="sub header">
            @
            
              Lindixuan
            
            · Sunday, Sep 20, 2020
            · 1 minute read
            · Update at Sep 20, 2020
          </div>
        </h1>
      </header>

      
        <img class="ui image" style="margin-top: 2rem;" src="" />
      

      <article style="margin-top: 2rem;"><p>可以大致将整个程序的主结构分为开始、操作、结束
再对操作项进行分析，可以将其拆分为得码（输入），解码（case），操作三部分。<br>
得码项使用scanf，解码则用switch函数完成，为了应对不同的操作，因此设置一个分支函数进行封装。<br>
显然根据游戏规则可知，蛇移动共分为三种情况，一是撞墙或吃自己导致失败，二是单纯移动，三是遇到食物进行吞食同时移动（同时设置获胜条件)。因此在remove函数中加入一个参数来区分移动与吞食移动。<br>
在移动时将身体与头部移动分离，再根据传入的参数判断是否吞食，移动操作完成。
同时还有随机产生事物，显示画面等功能也一一拆分成函数，最后将所有函数整合即可完成本次实验。</p>
<h2 id="打印地图">打印地图</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/printMap.png" alt="snake"></p>
<h2 id="蛇的构建">蛇的构建</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/body.png" alt="snake"></p>
<h2 id="控制">控制</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/Control1.png" alt="snake"></p>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/Control2.png" alt="snake"></p>
<h2 id="死亡条件">死亡条件</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/die.png" alt="snake"></p>
<h2 id="创造食物">创造食物</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/food.png" alt="snake"></p>
<h2 id="键盘控制蛇移动">键盘控制蛇移动</h2>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/move1.png" alt="snake"></p>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/move2.png" alt="snake"></p>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/move3.png" alt="snake"></p>
<p><img src="https://lindixuan.gitee.io/post/assets/%E8%B4%AA%E5%90%83%E8%9B%87/move4.png" alt="snake"></p>
<p><a href="https://lindixuan.gitee.io/%E8%B4%AA%E5%90%83%E8%9B%87%E4%BB%A3%E7%A0%81/">代码</a></p></article>
    </section>

    <footer class="ui attached segment dream-tags">
      
          <a class="ui label">No Tags</a>
      
      <div
        class="ui label"
        style="float: right; cursor: pointer;"
        onclick="savePostAsImg()">
        <i class="save icon"></i>Save as image
      </div>
    </footer>

    

    
    
    

  </div>
  <div class="sixteen wide mobile sixteen wide tablet four wide computer column">
    <article class="dream-header">
  <section class="ui top attached center aligned segment">
    <div class="ui small circular image">
      
        <img src="/img/sleep.jpg">
      
    </div>

    
    <h1 class="ui medium header">Lindixuan&#39;s blog<div class="sub header" style="margin-top: 0.5rem;">好耶！</div>
    </h1>
    

    <div class="ui horizontal list">
      
      <a class="item" href="/tags">
        <i class="tags icon" title="All Tags"></i>
      </a>
      <a class="item" href="/categories">
        <i class="th list icon" title="All Categories"></i>
      </a>
    </div>
  </section>

  

  

  <section class="ui attached segment header-socials">
    <nav class="ui secondary menu dream-menu dream-socials">
  
  
    <div class="item">
      <a href="mailto:1228210482@qq.com">
        <i class=" mail icon" title="Email"></i>
      </a>
    </div>
  

  

  

  

  

  

  

  
</nav>

  </section>

  <section class="ui bottom attached center aligned segment">
    
      <p>© 2020 - 2021 Hugo Theme Dream</p>
    

    <p>Powered by <a href="https://gohugo.io/" target="_blank">Hugo</a> with theme <a href="https://github.com/g1eny0ung/hugo-theme-dream" target="_blank">Dream</a>.</p>

    
  </section>
</article>

  </div>
</div>

          </div>
        </section>
        <section class="back">
          <div class="dream-max-width">
            <div class="ui centered relaxed grid dream-grid dream-back">
  
  
  

  <section class="sixteen wide mobile eight wide tablet four wide computer column dream-column">
    <article>
      <div class="ui top attached segment">
        <h3 class="ui header">Social Links</h3>
      </div>
      <div class="ui bottom attached segment">
        <nav class="ui secondary menu dream-menu dream-socials">
  
  
    <div class="item">
      <a href="mailto:1228210482@qq.com">
        <i class="large mail icon" title="Email"></i>
      </a>
    </div>
  

  

  

  

  

  

  

  
</nav>

      </div>
    </article>
  </section>

  <section class="sixteen wide mobile eight wide tablet four wide computer column dream-column">
    
  </section>

  
    
  
</div>

          </div>
        </section>
      </div>
    </div>

    <script>
  window.darkNav =  true 
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/semantic.min.js"></script>
<script src="/js/jquery.overlayScrollbars.min.js"></script>
<script src="/js/header.js"></script>
<script src="/js/main.js"></script>
<script src="/js/theme.js"></script>

    
<script src="/js/html2canvas.min.js"></script>
<script src="/js/post.js"></script>


  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/highlight.min.js"></script>

  
    
      <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/languages/ocaml.min.js"></script>
     
  

  <script>
  hljs.initHighlightingOnLoad()
  setHighlightTheme()

  function setHighlightTheme() {
    var isDark = localStore.getItem('hugo-theme-dream-is-dark')
    var lightTheme = "gruvbox-light"
    var darkTheme = "gruvbox-dark"
    var theme = isDark ? darkTheme : lightTheme

    $('link[data-highlight]').attr('href', 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/styles/' + theme + '.min.css')
    $('pre').css('background', isDark ? '#333' : '')
  }
</script>





    
      <div class="ui inverted segment" id="dream-search">
  <div class="ui search">
    <div class="ui transparent input">
      <input class="prompt" type="text" placeholder="Search" />
    </div>
    <div class="results"></div>
  </div>
</div>
<script>
  $(document).ready(function () {
    $.getJSON('https:\/\/lindixuan.gitee.io/index.json', function (data) {
      $('.ui.search').search({
        source: data,
        searchFields: ['title'],
        showNoResults: true,
      })
    })
  })
</script>
<script src="/js/search.js"></script>

    

    
  </body>
</html>
